package club.zhcs.captrue.module.dept;

import org.nutz.dao.entity.Record;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import club.zhcs.capture.bean.dept.Project;
import club.zhcs.capture.biz.auth.IUserService;
import club.zhcs.capture.biz.dept.IProjectService;
import club.zhcs.capture.biz.dept.ISubsidiaryService;
import club.zhcs.capture.vo.Consts.PermissionGroup;

import com.dgj.nutz.module.base.AbstractBaseModule;
import com.dgj.utils.entries.Result;
import com.dgj.utils.web.pager.Pager;

@At("/project")
public class ProjectModule extends AbstractBaseModule {

	@Inject
	ISubsidiaryService subsidiaryService;
	@Inject
	IUserService userService;
	@Inject
	IProjectService projectService;

	@Override
	public String _getNameSpace() {
		return PermissionGroup.STRUTS_ADMIN.getNameSpace();
	}

	@At
	@Ok("vel:project/list.html")
	public Result list(int page) {
		page = _fixPage(page);
		Pager<Record> pager = projectService.listByPage(page);
		pager.setUrl(_base() + "/project/list");
		return Result.success().addData("pager", pager);
	}

	@At
	@Ok("vel:project/list.html")
	public Result search(int page, String key) {
		page = _fixPage(page);
		key = _fixSearchKey(key);
		Pager<Record> pager = projectService.searchByPage(page, key);
		pager.setUrl(_base() + "/project/search");
		pager.addParas("key", key);
		return Result.success().addData("pager", pager).addData("key", key);
	}
	
	@At
	public Result listBySubsidiaryId(int id) {
		return Result.success().addData("projects", projectService.listBySubsidiaryId(id));
	}

	@At
	@GET
	@Ok("vel:project/add.html")
	public Result add() {
		return Result.success().addData("subsidiaries", subsidiaryService.listAll()).addData("users", userService.listAll());
	}
	@At("/edit/*")
	@GET
	@Ok("vel:project/add.html")
	public Result edit(int id) {
		return Result.success().addData("subsidiaries", subsidiaryService.listAll()).addData("users", userService.listAll()).addData("project", projectService.getAllInfo(id));
	}

	@At
	@POST
	public Result add(@Param("..") Project project) {
		return projectService.save(project);
	}
	@At
	@POST
	public Result edit(@Param("..") Project project) {
		return projectService.edit(project);
	}

	@At
	public Result delete(int id) {
		return projectService.delete(id) ? Result.success() : Result.fail("删除项目部失败!");
	}
	
}
